<?php
/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class AdminUserIdentity extends CUserIdentity
{
	private $_id;
	private $_name;
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
		$Criteria=new CDbCriteria();
		$Criteria->addCondition('username=:username');
		$Criteria->addCondition('email=:email','OR');
		$Criteria->params=array(':username'=>$this->username,':email'=>$this->username);
		$model = AdminMember::model()->find($Criteria);
		if(isset(Yii::app()->session['type']) && !empty(Yii::app()->session['type'])){
			if(!isset($model)){
				$this->errorCode=self::ERROR_USERNAME_INVALID;
			}else{
				$this->errorCode=self::ERROR_NONE;
			}
			unset(Yii::app()->session['type']);
		}else{
			if(!isset($model)){
				$this->errorCode=self::ERROR_USERNAME_INVALID;
			}else if(trim($model->password)!=md5(md5($this->password).$model->salt)){
				$this->errorCode=self::ERROR_PASSWORD_INVALID;
			}else{
				$this->errorCode=self::ERROR_NONE;
			}
		}
		if($model){
			$this->_name=$model->username;
			$this->_id=$model->uid;
		}
		return !$this->errorCode;
	}
	//设置用户名
	public function getName(){
		return $this->_name;
	}
	//设置昵称
	public function getId()
    {
        return $this->_id;
    }
}